﻿using MigraDoc.DocumentObjectModel;
using MigraDoc.DocumentObjectModel.Shapes;
using MigraDoc.Rendering;
using PdfSharp.Pdf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GedeaProject.Business.PdfWriter
{
    public class PdfClass1
    {
        public void CreatePdf()
        {
            // Create a MigraDoc document
            Document document = CreateDoc();
            document.UseCmykColor = true;

            // ===== Unicode encoding and font program embedding in MigraDoc is demonstrated here =====

            // A flag indicating whether to create a Unicode PDF or a WinAnsi PDF file.
            // This setting applies to all fonts used in the PDF document.
            // This setting has no effect on the RTF renderer.
            const bool unicode = false;

            // An enum indicating whether to embed fonts or not.
            // This setting applies to all font programs used in the document.
            // This setting has no effect on the RTF renderer.
            // (The term 'font program' is used by Adobe for a file containing a font. Technically a 'font file'
            // is a collection of small programs and each program renders the glyph of a character when executed.
            // Using a font in PDFsharp may lead to the embedding of one or more font programms, because each outline
            // (regular, bold, italic, bold+italic, ...) has its own fontprogram)
            const PdfFontEmbedding embedding = PdfFontEmbedding.Always;

            // ========================================================================================

            // Create a renderer for the MigraDoc document.
            PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(unicode, embedding);

            // Associate the MigraDoc document with a renderer
            pdfRenderer.Document = document;

            // Layout and render document to PDF
            pdfRenderer.RenderDocument();

            // Save the document...
            const string filename = "C:\\Users\\Ignacio\\Desktop\\Test.pdf";
            pdfRenderer.PdfDocument.Save(filename);
        }

        public Document CreateDoc()
        {
            Document document = new Document();
            Section section;
            Paragraph paragraph;

            Style style = document.Styles["Normal"];
            style.Font.Color = new Color(102, 102, 102);
            style.ParagraphFormat.SpaceAfter = "0.5cm";

            document.Info.Title = "Pending PINs report";
            document.Info.Subject = "Report of letters pending to send to customers with their PINs.";
            document.Info.Author = "Ignacio";

            section = document.AddSection();
            section.PageSetup.RightMargin = "4cm";
            section.PageSetup.LeftMargin = "4cm";
            //section = document.LastSection;
            paragraph = section.AddParagraph();
            paragraph.Format.Alignment = ParagraphAlignment.Right;
            Image image = paragraph.AddImage("C:\\Users\\Ignacio\\Documents\\Visual Studio 2013\\Projects\\Versioned\\trunk\\Gedea\\GedeaOld2012\\Gedea.WinForm\\gedea icono.ico");
            image.Width = "4cm";


            paragraph = section.AddParagraph("Dear " + "@CustomerName" + ",", "Heading1");
            paragraph = section.AddParagraph("Thank you for choosing WebLock to protect all of your domains currently under management in account number " + "@AccountId" + ".", "Heading1");
            paragraph = section.AddParagraph("Your unique PIN for this program is: " + "@Pin", "Heading1");
            paragraph = section.AddParagraph("Please note that your PIN will be required to authenticate you as a designated certified user for this account.", "Heading1");
            paragraph = section.AddParagraph("At " + "@CompanyName" + ", we take your security very seriously. Thank you for helping us protect you better. If you have any questions, please contact our WebLock Services team by phone at " + "@CompanyPhone" + " or email at " + "@CompanyEmail" + ".");
            paragraph = section.AddParagraph("Regards,", "Heading1");
            paragraph = section.AddParagraph("WebLock Services", "Heading1");
            paragraph.Format.SpaceAfter = "0cm";
            paragraph = section.AddParagraph("@CompanyName", "Heading1");
            
            return document;
        }
    }
}
